package com.woniu.controller;

import com.woniu.entity.Account;
import com.woniu.service.AccountService;
import com.woniu.util.JwtUtil;
import com.woniu.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@Slf4j
public class AccountController {
    @Resource
    AccountService accountService;

    @Resource
    JwtUtil jwtUtil;

    @PostMapping("/api/login")
    public R login(Account account, HttpServletResponse response) {

        log.info("用户名：" + account.getAccountName() + ",密码：" + account.getPassword());

        R r = new R();

        Account temp = accountService.findAccountByLogin(account.getAccountName());
        if (temp != null) {
            if (temp.getPassword().equals(account.getPassword())) {
                r.setCode(200);
                r.setMsg("登录成功!");
                r.setData(temp);
                // 登录成功，生成token
                String token = jwtUtil.createToken(account.getAccountName());
                log.info("后端生成的token:" + token);
                // 把token存到响应头里面
                response.setHeader("authentication", token);
            } else {
                r.setCode(500);
                r.setMsg("对不起,密码有误！");
            }
        } else {
            r.setCode(500);
            r.setMsg("对不起,用户名有误！");
        }
        return r;
    }

    @PostMapping("/api/register")
    public R register(Account account) {
        log.info("注册信息：用户名 - " + account.getAccountName() + ", 密码 - " + account.getPassword());
        System.out.println(account);
        R r = new R();

        // 检查用户名是否已存在
        if (accountService.isAccountNameExists(account.getAccountName())) {
            r.setCode(500);
            r.setMsg("用户名已存在，请更换！");
            return r;
        }

        // 执行注册逻辑
        boolean success = accountService.registerAccount(account);

        if (success) {
            r.setCode(200);
            r.setMsg("注册成功！");
            r.setData(account);
        } else {
            r.setCode(500);
            r.setMsg("注册失败，请稍后再试！");
        }

        return r;
    }


    @GetMapping("/account/{accountId}")
    public R searchByAccountId(@PathVariable int accountId) {
        Account accountById = accountService.findAccountById(accountId);
        return R.build(200, "searchByAccountId", accountById);
    }


    @PostMapping("/account/updateAccount")
    public R updateAccountByAccountId(@RequestBody Account account) {
        boolean b = accountService.updateAccountByAccountId(account);
        if (b) {
            return R.build(200, "添加成功", account);
        } else {
            return R.build(500, "添加失败", null);
        }
    }

    @PostMapping("/account/updatePassword")
    public R updatePasswordByAccountId(@RequestBody Account account) {
        boolean b = accountService.updatePasswordByAccountId(account);
        if (b) {
            return R.build(200, "密码修改成功", account);
        } else {
            return R.build(500, "密码修改失败", null);
        }
    }


}


